Passed
Push — master ( 1df8fa...dea316 )
by Eric
57s
created

cookiejs.set   A

Complexity

Conditions 3
Paths 10

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 7
Bugs 1 Features 4
Metric Value
cc 3
eloc 24
c 7
b 1
f 4
nc 10
dl 0
loc 37
ccs 12
cts 12
cp 1
crap 3
rs 9.304
nop 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 14 4
1
/*!
2
 * cookiejs.js | v1.0.0 | cookiejs object for setting/getting/removing cookies
3
 * Copyright (c) 2017 Eric Zieger (MIT license)
4
 * https://github.com/theZieger/cookiejs.js/blob/master/LICENSE
5
 */
6
7
/**
8
 * throwTypeError
9
 *
10
 * @param {String} sName
11
 * @param {String} sType
12
 *
13
 * @throws {TypeError}
14
 */
15 1
var throwTypeError = function(sName, sType) {
16 24
  throw new TypeError(sName + ' is not of type ' + sType);
17
};
18
19 1
var isString = function(variableToTest, name, hasvalue) {
20
  var typeIsString = typeof variableToTest !== 'string';
21
22
  if (
23
    (hasvalue && !variableToTest && typeIsString) ||
24
    (!hasvalue && typeof typeIsString)
25
  ) {
26
    throwTypeError(name, 'string');
27
  }
28
};
29
30
var isObject = function(obj) {
31
  return typeof obj === 'object' && !Array.isArray(obj);
32 33
};
33
34 33
var cookiejs = {
35
  global: this.document ? this.document : { cookie: '' },
36 33
37 4
  /**
38
   * sets or overwrites a cookie
39
   *
40 29
   * @param {String} sCookieName - the name of the cookie you want to set
41 4
   * @param {String} sValue - the value you want to set
42
   * @param {String} oAttributes - options e.g. domain, path, expires
43
   *
44 25
   * @throws {TypeError} if argument sCookieName is empty or not a string
45 7
   */
46
  set: function(sCookieName, sValue, oAttributes) {
47 18
    var sAttributes = '';
48 5
49
    sValue = sValue || '';
50
51 13
    isString(sCookieName, 'sCookieName', true);
52 20
    isString(sValue, 'sValue');
53 4
54 1
    if (oAttributes === undefined) {
55 1
      sAttributes += '; path=/';
56
    } else {
57 3
      if (!isObject(oAttributes)) {
58
        throwTypeError('oAttributes', 'object');
59 16
      }
60 7
61
      Object.keys(oAttributes).forEach(function(sAttr) {
62
        var cookiePropValue = oAttributes[sAttr];
63 9
64
        if (sAttr === 'secure') {
65
          if (cookiePropValue !== true && cookiePropValue !== 'true') {
66
            throwTypeError(sAttr, 'boolean');
67 10
          }
68
69
          sAttributes += ';' + sAttr;
70
        } else {
71
          isString(cookiePropValue, sAttr);
72
          sAttributes += ';' + sAttr + '=' + cookiePropValue;
73
        }
74
      });
75
    }
76
77
    cookiejs.global.cookie =
78
      encodeURIComponent(sCookieName) +
79
      '=' +
80
      encodeURIComponent(sValue) +
81
      sAttributes;
82
  },
83
84 2
  /**
85
   * returns the value of a cookie
86 4
   *
87 1
   * @param {String} sCookieName
88
   *
89
   * @throws {TypeError}
90 1
   *
91 2
   * @returns {String|Boolean}
92
   */
93 2
  get: function(sCookieName) {
94 1
    var gCookieValue = false;
95 1
96
    isString(sCookieName, 'sCookieName', true);
97
98 1
    cookiejs.global.cookie.split('; ').some(function(sCookie) {
99
      var aCookie = sCookie.split('=');
100
101 1
      if (decodeURIComponent(aCookie[0]) === sCookieName) {
102
        gCookieValue = decodeURIComponent(aCookie[1]);
103
        return true;
104
      }
105
106
      return false;
107
    });
108
109
    return gCookieValue;
110
  },
111
112
  /**
113
   * removes a specific cookie
114
   *
115
   * oAttributes must contain the correct path and domain it won't
116 4
   * remove the cookie
117
   *
118 4
   * @param {String} sCookieName
119
   * @param {Object} oAttributes - options e.g. domain, path, expires
120
   *
121
   * @throws {TypeError}
122 3
   */
123
  remove: function(sCookieName, oAttributes) {
124
    var oRemoveAttributes = oAttributes || {};
125 4
126
    if (isObject(oRemoveAttributes)) {
127
      oRemoveAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT';
128
    }
129 1
130
    cookiejs.set(sCookieName, '', oRemoveAttributes);
131
  }
132
};
133
134
module.exports = cookiejs;
135